package com.doctor.ui.homedoctor;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.bean.PatientFileBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.DialogHelper.DialogSelectCallback;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;

public class ModifyPatientFileFrag extends BaseFragment{

	/**
	 * 修改患者档案
	 */
	
	private Context context;
	private Calendar calendar;
	private TextView number,birthDay,endDay,familyBtn,personalBtn,diseaseBtn,bloodType;
	private EditText name,email,address,phone,mobile,body,contacts, contactsMobile, allergy, content;
	private RadioButton marrigeNo, marrigeYes, male,female;
	private LinearLayout saveBtn,cancelBtn;
	private Click click;
	//存储家族史、个人史、疾病史内容
	private String[] content_txt;
	//用来标识按钮，0家族史，1个人史，2疾病史
	private int tag;
	private PatientFileBean bean;
	private String[] bloodItems={"A型","B型","AB型","O型","未知"};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context=getActivity();
		click=new Click();
		bean=(PatientFileBean) getArguments().getSerializable(ConstConfig.BEAN);
		content_txt=new String[3];
		content_txt[0]=bean.getFamilyHistory();
		content_txt[1]=bean.getPersonalHistory();
		content_txt[2]=bean.getDiseaseHistory();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.modify_patient_file, container, false);
		calendar=Calendar.getInstance();
		initiaView(view);
		
		return view;
	}
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		number=(TextView) view.findViewById(R.id.number);
		int maxId=DbOperator.getInstance(context).getMaxId(ConstConfig.PATIENT_FILE_TABLE);
		number.setText(bean.getId()+"");
		
		birthDay=(TextView) view.findViewById(R.id.birthday);
		birthDay.setOnClickListener(click);
		birthDay.setText(bean.getBirthDay());
		
		endDay=(TextView) view.findViewById(R.id.endday);
		endDay.setOnClickListener(click);
		endDay.setText(bean.getEndDay());
		
		familyBtn=(TextView) view.findViewById(R.id.familyHistory);
		familyBtn.setBackgroundResource(R.color.green);
		familyBtn.setOnClickListener(click);
		
		personalBtn=(TextView) view.findViewById(R.id.personalHistory);
		personalBtn.setBackgroundResource(R.color.light_green);
		personalBtn.setOnClickListener(click);
		tag=0;
		
		diseaseBtn=(TextView) view.findViewById(R.id.diseaseHistory);
		diseaseBtn.setBackgroundResource(R.color.light_green);
		diseaseBtn.setOnClickListener(click);
		
		name=(EditText) view.findViewById(R.id.name);
		name.setText(bean.getName());
		
		email=(EditText) view.findViewById(R.id.email);
		email.setText(bean.getEmail());
		
		address=(EditText) view.findViewById(R.id.address);
		address.setText(bean.getAddress());
		
		phone=(EditText) view.findViewById(R.id.phone);
		phone.setText(bean.getPhone());
		
		mobile=(EditText) view.findViewById(R.id.mobile);
		mobile.setText(bean.getMobile());
		
		body=(EditText) view.findViewById(R.id.body);
		body.setText(bean.getBody());
		
		bloodType=(TextView) view.findViewById(R.id.bloodType);
		bloodType.setText(bean.getBloodType());
		bloodType.setOnClickListener(click);
		
		contacts = (EditText) view.findViewById(R.id.contacts);
		String contactsText = bean.getContacts();
		if (contactsText != null)
			contacts.setText(contactsText);
        contactsMobile = (EditText) view.findViewById(R.id.contacts_mobile);
        String contactsMobileText = bean.getContactsMobile();
        if (contactsMobileText != null)
			contactsMobile.setText(contactsMobileText);
        allergy = (EditText) view.findViewById(R.id.allergy);
        String allergyText = bean.getAllergy();
        if (allergyText != null)
			allergy.setText(allergyText);
		
		content=(EditText) view.findViewById(R.id.content);
		//初始化显示家族史内容
		content.setText(content_txt[tag]);
		
		marrigeNo = (RadioButton) view.findViewById(R.id.marrige_no);
		marrigeYes = (RadioButton) view.findViewById(R.id.marrige_yes);
		String marriageText = bean.getMarriage();
		if (marriageText!=null && "未婚".equals(marriageText)) {
			marrigeNo.setChecked(true);
		} else if (marriageText!=null && "已婚".equals(marriageText)) {
			marrigeYes.setChecked(true);
		}
		
		male=(RadioButton) view.findViewById(R.id.male);
		female=(RadioButton) view.findViewById(R.id.female);
		String sex_txt=bean.getSex();
		if (sex_txt!=null && "男".equals(sex_txt.trim())) {
			male.setChecked(true);
		} else if (sex_txt!=null && "女".equals(sex_txt.trim())) {
			female.setChecked(true);
		}
		
		saveBtn=(LinearLayout) view.findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(click);
		
		cancelBtn=(LinearLayout) view.findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(click);
	}
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v==birthDay) {
				DialogHelper.showDatePickerDialog(context, birthDay, calendar, false);
			} else if (v==endDay) {
				DialogHelper.showDatePickerDialog(context, endDay, calendar, false);
			} else if (v==bloodType) {
				DialogHelper.selectDialog(context, "选择血型", bloodItems, new DialogSelectCallback() {					
					@Override
					public void onDialogSelected(int position) {
						// TODO Auto-generated method stub
						bloodType.setText(bloodItems[position]);
					}
				});
			} else if (v==familyBtn) {
				familyBtn.setBackgroundResource(R.color.green);
				personalBtn.setBackgroundResource(R.color.light_green);
				diseaseBtn.setBackgroundResource(R.color.light_green);
				getContent(tag);
				tag=0;
				content.setText(content_txt[tag]);
			} else if (v==personalBtn) {
				familyBtn.setBackgroundResource(R.color.light_green);
				personalBtn.setBackgroundResource(R.color.green);
				diseaseBtn.setBackgroundResource(R.color.light_green);
				getContent(tag);
				tag=1;
				content.setText(content_txt[tag]);
			} else if (v==diseaseBtn) {
				familyBtn.setBackgroundResource(R.color.light_green);
				personalBtn.setBackgroundResource(R.color.light_green);
				diseaseBtn.setBackgroundResource(R.color.green);
				getContent(tag);
				tag=2;
				content.setText(content_txt[tag]);
			} else if (v==cancelBtn) {
				getActivity().getSupportFragmentManager().popBackStack();
			} else if (v==saveBtn) {
				String name_txt=name.getText().toString().trim();
				if (name_txt==null || "".equals(name_txt)) {
					Toast.makeText(context, "请填写姓名！", Toast.LENGTH_SHORT).show();
					return;
				}
				ContentValues values=new ContentValues();
				values.put("_name", name_txt);
				
				String  email_txt=email.getText().toString().trim();
				values.put("_email", email_txt);

				if (marrigeNo.isChecked()) {
					values.put("_marriage", "未婚");
				} else if (marrigeYes.isChecked()) {
					values.put("_marriage", "已婚");
				}
				
				if (male.isChecked()) {
					values.put("_sex", "男");
				} else if (female.isChecked()) {
					values.put("_sex", "女");
				}
				
				String birthDay_txt=birthDay.getText().toString();
				values.put("_birth_day", birthDay_txt);
				
				String endDay_txt=endDay.getText().toString();
				values.put("_end_day", endDay_txt);
				
				String address_txt=address.getText().toString().trim();
				values.put("_address", address_txt);
					
				String phone_txt=phone.getText().toString().trim();
				values.put("_phone", phone_txt);
				
				String mobile_txt=mobile.getText().toString().trim();
				values.put("_mobile", mobile_txt);
				
				String body_txt=body.getText().toString().trim();
				values.put("_body", body_txt);
				
				String bloodType_txt=bloodType.getText().toString().trim();
				values.put("_blood_type", bloodType_txt);
				
				String contacts_txt = contacts.getText().toString().trim();
				values.put("_contacts", contacts_txt);
				
				String contacts_mobile = contactsMobile.getText().toString().trim();
				values.put("_contacts_mobile", contacts_mobile);
				
				String allergy_text = allergy.getText().toString().trim();
				values.put("_allergy", allergy_text);
				
				getContent(tag);
				values.put("_family_hitory", content_txt[0]);				
				values.put("_personal_hitory", content_txt[1]);				
				values.put("_disease_history", content_txt[2]);
				values.put("_operation", ConstConfig.OPERATION_MODIFY);
				
				int row=DbOperator.getInstance(context).updateData(ConstConfig.PATIENT_FILE_TABLE, values, bean.getId());
				if (row>0) {
					Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
					getActivity().getSupportFragmentManager().popBackStack();
				} else {
					Toast.makeText(context, "修改失败！", Toast.LENGTH_SHORT).show();
				}
			}
		}
		
	}
	
	
	/**
	 * 切换按钮保存文字信息
	 * @param tag
	 */
	private void getContent(int tag){
		String contentString=content.getText().toString().trim();
		
		switch (tag) {
		case 0:
			content_txt[0]=contentString;
			break;

		case 1:
			content_txt[1]=contentString;
			break;
			
		case 2:
			content_txt[2]=contentString;
		}
	}	
	
	
	
	
	
}
